import { logger } from '$common';
import { GlobalBuses } from './eventBus';
import { simulationJobService } from '$domain/model/simulation/service/jobService';
import { buildAbilities, registerToGateway } from '$domain/model/agent/agentService';
import { DriverReloadedEvent, DriverReleasedEvent } from '$domain/event';
export class MonitorService {

  init() {
    GlobalBuses.DriverEvent.subscribe(DriverReloadedEvent.NAME, async () => {
      logger.debug(`🚀 The ${DriverReloadedEvent.NAME} event is triggered.`);
      try {
        const changedAbilities = buildAbilities();
        await registerToGateway(changedAbilities);
        logger.debug('😄 update abilities to gateway successfully.');
      } catch (e) {
        logger.error(`Error during register abilities to gateway, e: ${e}`);
      }
    });

    GlobalBuses.DriverEvent.subscribe(DriverReleasedEvent.NAME, async (e: DriverReleasedEvent) => {
      logger.debug(`🚀 The ${DriverReleasedEvent.NAME} event is triggered.`);
      const productName = e.driverName;
      await simulationJobService.runFromQueue(productName);
    });
  }
}